package com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.query;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.AbstractWrapper;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.segments.MergeSegments;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.toolkit.EmptyUtils;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.toolkit.StringPool;

/**
 * <p>
 * Entity 对象封装操作类
 * </p>
 *
 * @author Start
 */
public class QueryWrapper extends AbstractWrapper<String, QueryWrapper> {

    /**
     * 查询字段
     */
    private String sqlSelect;

    public QueryWrapper() {
        this.initNeed();
    }

    public QueryWrapper(String... columns) {
        this.select(columns);
        this.initNeed();
    }

    /**
     * 非对外公开的构造方法,只用于生产嵌套 sql
     * @param paramNameSeq
     * @param paramNameValuePairs
     * @param mergeSegments
     */
    private QueryWrapper(AtomicInteger paramNameSeq,
                         Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
    }

    public QueryWrapper select(String... columns) {
        if (EmptyUtils.isNotEmpty(columns)) {
            this.sqlSelect = String.join(StringPool.COMMA, columns);
        }
        return typedThis;
    }

    @Override
    public String getSqlSelect() {
        return sqlSelect;
    }

    @Override
    protected String columnToString(String column) {
        return column;
    }

    /**
     * <p>
     * 用于生成嵌套 sql
     * 故 sqlSelect 不向下传递
     * </p>
     */
    @Override
    protected QueryWrapper instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
        return new QueryWrapper(paramNameSeq, paramNameValuePairs, new MergeSegments());
    }
}
